// AccountDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VaxVoIPSample.h"
#include "AccountDlg.h"
#include "afxdialogex.h"
#include "VaxSIPUserAgent.h"


IMPLEMENT_DYNAMIC(CAccountDlg, CPropertyPage)

CAccountDlg::CAccountDlg(CVaxSIPUserAgent* pVaxSIPUserAgent) : CPropertyPage(CAccountDlg::IDD)
{
	m_pVaxSIPUserAgent = pVaxSIPUserAgent;
	
	m_sAccountUserName = "";
	m_sAccountDisplayName = "";
	m_sAccountAuthName = "";
	m_sAccountPassword = "";
	m_sAccountDomainSIP = "";
	m_sAccountProxySIP = "";
	
	m_nTotalLines = 10;
	roadFromConfig();

	m_bCheckRegisterToProxy = TRUE;
	m_bCheckCaptureSoundDevic = TRUE;
	
	m_psp.dwFlags &= ~PSP_HASHELP; 

	m_objFont.CreateFont(14, 0, 0, 0, FW_BOLD, FALSE, FALSE, 0, ANSI_NULL, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Verdana");
}

CAccountDlg::~CAccountDlg()
{

}

void CAccountDlg::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);

	DDX_Control(pDX, IDC_BTN_ONLINE, m_ctrlInitBtn);
	DDX_Text(pDX, IDC_EDIT_USER_NAME, m_sAccountUserName);
	DDX_Text(pDX, IDC_EDIT_DISPLAY_NAME, m_sAccountDisplayName);
	DDX_Text(pDX, IDC_EDIT_AUTH_LOGIN, m_sAccountAuthName);
	DDX_Text(pDX, IDC_EDIT_AUTH_PASSWORD, m_sAccountPassword);
	DDX_Text(pDX, IDC_EDIT_DOMAIN, m_sAccountDomainSIP);
	DDX_Text(pDX, IDC_EDIT_SIP_PROXY, m_sAccountProxySIP);
	DDX_Text(pDX, IDC_EDIT_LINES_TOTAL, m_nTotalLines);
	DDX_Check(pDX, IDC_CHECK_REGISTER_SIP_PROXY, m_bCheckRegisterToProxy);
	DDX_Check(pDX, IDC_CHECK_CAPTURESOUND_DEVIC, m_bCheckCaptureSoundDevic);
	DDX_Control(pDX, IDC_LIST_STATUSLOG, m_ctrlStatusLogListBox);

	GetDlgItem(IDC_GROUP_SIP_ACCOUNTSETTING)->SetFont(&m_objFont);
	GetDlgItem(IDC_GROUP_ACCOUNT_STATUSLOG)->SetFont(&m_objFont);
	
}

BEGIN_MESSAGE_MAP(CAccountDlg, CPropertyPage)
	ON_BN_CLICKED(IDC_BTN_ONLINE, &CAccountDlg::OnBtnClickedOnLine)
	ON_BN_CLICKED(IDC_BTN_ACCOUNT_CLEAR, &CAccountDlg::OnBtnClickedClearLog)
END_MESSAGE_MAP()

void CAccountDlg::OnBtnClickedOnLine()
{
	CString sInitBtnText = "";
	m_ctrlInitBtn.GetWindowText(sInitBtnText);

	if(sInitBtnText == "Offline")
	{
		m_ctrlInitBtn.SetWindowText("Online");
		m_pVaxSIPUserAgent->UnInitialize();
        return;
    }
       
    UpdateData(TRUE);

	if(m_sAccountAuthName.IsEmpty())
	{
		MessageBox("Please enter the SIP proxy auth Login.", "SIP Account settings", MB_OK|MB_ICONEXCLAMATION);
		return;
	}

	if(m_sAccountProxySIP.IsEmpty())
	{
		MessageBox("Please enter the SIP proxy address or URI.", "SIP Account settings", MB_OK|MB_ICONEXCLAMATION);
		return;
	}
    if (m_nTotalLines <= 0)
    {
        MessageBox("Total number of lines must be greater than 0.", "VaxVoIP SDK",MB_OK|MB_ICONEXCLAMATION);
        return;
    }
	    
    BOOL bResult = m_pVaxSIPUserAgent->VaxInitObject(m_sAccountUserName, m_sAccountAuthName, m_sAccountPassword, m_sAccountDisplayName, m_sAccountDomainSIP, m_sAccountProxySIP, m_bCheckCaptureSoundDevic, m_nTotalLines);

    if(!bResult)
	{
	    return;
	}

	if (m_bCheckRegisterToProxy)
    {
		AddToAccountLog("Registering to SIP Proxy.");
        if (!m_pVaxSIPUserAgent->RegisterToProxy(1800)) return;
    }
	
    m_pVaxSIPUserAgent->EnableKeepAlive(10);

	m_ctrlInitBtn.SetWindowText("Offline");
	saveToConfig();
}

void CAccountDlg::OnBtnClickedClearLog()
{
	m_ctrlStatusLogListBox.ResetContent();
}

void CAccountDlg::AddToAccountLog(CString sAccountLog)
{
	m_ctrlStatusLogListBox.AddString(sAccountLog);
}

//////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////

void CAccountDlg::OnTryingToRegister()
{
    AddToAccountLog("Trying to register.");
}

void CAccountDlg::OnFailToRegister()
{
    AddToAccountLog("Fail to Register");
}

void CAccountDlg::OnFailToRegisterEx(int nStatusCode, CString sReasonPhrase)
{
    AddToAccountLog("Failed: " + sReasonPhrase);
}

void CAccountDlg::OnSuccessToRegister()
{
    AddToAccountLog("Success to register.");
}

void CAccountDlg::OnTryingToReRegister()
{
	AddToAccountLog("Trying to Re-Register.");	
}

void CAccountDlg::OnFailToReRegister()
{
    
}

void CAccountDlg::OnFailToReRegisterEx(int nStatusCode, CString sReasonPhrase)
{
    AddToAccountLog("Failed: " + sReasonPhrase);
}

void CAccountDlg::OnSuccessToReRegister()
{
    AddToAccountLog("Success to Re-Register.");
}

void CAccountDlg::OnTryingToUnRegister()
{
    AddToAccountLog("Trying to Unregister.");
}

void CAccountDlg::OnFailToUnRegister()
{
    AddToAccountLog("Fail to unregister.");
}

void CAccountDlg::OnSuccessToUnRegister()
{
    AddToAccountLog("Unregistered successfully.");
}

BOOL CAccountDlg::OnApply()
{
	return FALSE;
	//return CPropertyPage::OnApply();
}

void CAccountDlg::saveToConfig()
{
	
}

void CAccountDlg::roadFromConfig()
{
	
}
